$(function(){
  
  $('.citySearch').click(function(){
        var cityname = $('input[name="citySearch"]').val();
        var sel = document.getElementById('citySelectList');
        var isMatch = 0;
        if(cityname.trim() == ''){
          $('#citySelectList option').show();
          return false;
        }
        $('#citySelectList option').each(function(){
          var city = $(this).text();
          var city_val = $(this).val();
          if(city.indexOf(cityname) == -1){
            $(this).hide();
          }else{
            $(this).show();
            if(!isMatch) $('#citySelectList').val(city_val);
            isMatch = 1;
          console.log($(this).text());
          }
      });
      if(!isMatch){
         $('#citySelectList option').eq(0).show();
         $('#citySelectList').val(0);
      }
      return false;

      });

    $('.shopSearch').click(function(){
      var shopname = $('input[name="searchName"]').val();
      var cityid  = $('#citySelectList_2').val()>0?$('#citySelectList_2').val()>0:258;
      $("#shop_list").html('<option value="0">不设置广告商家</option>');
      $.ajax({
        url:'/index.php?g=admin&m=activites&a=searchShop',
        type:"POST",
        data:{shopName:shopname,cityId:cityid},
        dataType:"JSON",
        success:function(data){
          console.log(data);
          if(data.shops.length>0) $("#shop_list").html('');
          $.each(data.shops,function(id,shop){
            var htm = '<option value="'+shop.shopId +'" data-manage-shopid="'+ shop.manageShopId +'">'+ shop.shopName+'(' + shop.branchName +')</option>';
            $('input[name="manageShopId"]').val(shop.manageShopId);
            $('input[name="shopName"]').val(shop.shopName+'(' + shop.branchName +')');
            $("#shop_list").append(htm);
            console.log(htm);
          });
        }});
      return false;
    });
    $('#shop_list').change(function(data){
      var mid = $('#shop_list option[value="' + $(this).val() +'"]').attr('data-manage-shopid');
      var shopname = $('#shop_list option[value="' + $(this).val() +'"]').text();
      $('input[name="manageShopId"]').val(mid);
      $('input[name="shopName"]').val(shopname);
      console.log($(this).val() + "   "+ shopname);
    });

    $('.citySearch_2').click(function(){
        var cityname = $('input[name="citySearch_2"]').val();
        var sel = document.getElementById('citySelectList_2');
        var isMatch = 0;
        if(cityname.trim() == ''){
          $('#citySelectList_2 option').show();
          return false;
        }
        $('#citySelectList_2 option').each(function(){
          var city = $(this).text();
          var city_val = $(this).val();
          if(city.indexOf(cityname) == -1){
            $(this).hide();
          }else{
            $(this).show();
            if(!isMatch) $('#citySelectList_2').val(city_val);
            isMatch = 1;
          console.log($(this).text());
          }
      });
      if(!isMatch){
         $('#citySelectList_2 option').eq(0).show();
         $('#citySelectList_2').val(0);
      }
      return false;

      });

      $('.addCity').click(function(){
        var cityid = $('#citySelectList option:selected').val();
        var cityname = $('#citySelectList option:selected').text();
        var trCont = '<tr><<td>'+ cityid+'</td><td>'+ cityname +'</td><td><a href="#" class="delCity">删除</a></td></tr>';
        $('.cityList').append(trCont).show();
        $('.cityids').append('<input type="hidden" data-cityid="'+cityid+'" name="citys[]" value="'+cityid+'"/>')
        return false;
      });

      $('.cityList .delCity').live('click',function(){
        console.log($(this).parents('tr').index());
        var $tr = $(this).parents('tr');
        var id = $tr.find('td').eq(0).text();
        $tr.remove();
        console.log($('input[data-cityid="'+id+'"]'));
        $('input[data-cityid="'+id+'"]').remove();


      });

      function addCityToSearchList(citys){
        $('#citySelectList').html('');
        for(city in citys){
          option = "<option value='"+ citys[city].cityid +"'>"+citys[city].name+"</option>";
          console.log(city);
          console.log(option);
          $('#citySelectList').append(option);
        }

        var option = '<option value="0">所有城市</option>';
        $('#citySelectList').append(option);
      }
});